﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using University.Workshop.Model;
using University.Workshop.DataAccess;
using System.Globalization;
using System.Collections.ObjectModel;
using System.Web;

namespace University.Workshop.Business
{
    public static class ProductRegistrationManager
    {
        public static Collection<HttpPostedFile> GetUploadedFiles(Collection<HttpPostedFile> imageAttempts)
        {
            Collection<HttpPostedFile> files = new Collection<HttpPostedFile>();
            if (imageAttempts != null)
            {
                try
                {
                    string fileExt;
                    foreach (HttpPostedFile image in imageAttempts)
                    {
                        if (!String.IsNullOrEmpty(image.FileName))
                        {
                            fileExt = Path.GetExtension(image.FileName).ToLower(CultureInfo.CurrentCulture);
                            if (fileExt != ".jpg" && fileExt != ".gif" && fileExt != ".bmp" && fileExt != ".jpeg" && fileExt != ".png")
                                throw new ValidationException("One or more files you are trying to upload aren't images");
                            else
                                files.Add(image);
                        }
                    }
                }
                catch (Exception)
                {
                    throw new ValidationException("One or more files you are trying to upload aren't images");
                }
            }
            return files;
        }

        public static Product ModifyProduct(
            string name,
            string price,
            string description,
            string category,
            bool hasImages,
            string currency,
            long productId,
            string status,
            string quantity
            )
        {
            string exceptions = String.Empty;
            float productPrice = 0;
            short productQuantity = 1;
            if (String.IsNullOrEmpty(name))
                exceptions += "<br />Product name can't be empty.";
            if (String.IsNullOrEmpty(price))
                exceptions += "<br />Product price can't be empty.";
            if (String.IsNullOrEmpty(description))
                exceptions += "<br />Product description can't be empty.";
            if (String.IsNullOrEmpty(quantity))
                exceptions += "<br />Product quantity can't be empty.";
            if (!String.IsNullOrEmpty(exceptions))
            {
                throw new ValidationException(exceptions);
            }
            var reg = new Regex(@"^[^< ]*[^>]*$");
            if (name != null)
            {
                if (!reg.IsMatch(name))
                {
                    exceptions += "<br />Product name contains illegal characters.";
                }
                if (name.Length < 10 || name.Length > 100)
                    exceptions += "<br />Product name must be between 10 and 100 characters.";
            }
            if (description != null)
            {
                if (!reg.IsMatch(description))
                {
                    exceptions += "<br />Product description contains illegal characters.";
                }
                if (description.Length < 50 || description.Length > 500)
                    exceptions += "<br />Product description must be between 50 and 500 characters.";
            }
            try
            {
                productPrice = float.Parse(price, CultureInfo.CurrentCulture);
                if (productPrice < 5 || productPrice > 50000)
                {
                    exceptions += "<br />Product price must be in range of (5-50000).";
                }
            }
            catch (FormatException)
            {
                exceptions += "<br />Product price is not a valid number.";
            }
            try
            {
                productQuantity = short.Parse(quantity, CultureInfo.CurrentCulture);
                if (productQuantity < 1 || productQuantity > 99)
                {
                    exceptions += "<br />Product quantity must be in range of (1-99).";
                }
            }
            catch (FormatException)
            {
                exceptions += "<br />Product quantity is not a valid number.";
            }
            if (!String.IsNullOrEmpty(exceptions))
            {
                throw new ValidationException(exceptions);
            }
            Product product = new Product
            {
                Name = name,
                Category = category,
                Currency = currency,
                Description = description,
                Price = productPrice,
                HasImages = hasImages,
                Postdate = DateTime.Now,
                ProductId = productId,
                Status = status,
                Quantity = productQuantity
            };
            product = ProductDataAccess.ModifyProduct(product);
            return product;
        }

        public static Product RegisterNewProduct(
            string name,
            string price,
            string description,
            string category,
            bool hasImages,
            string currency,
            long userId,
            string quantity          
            )
        {
            string exceptions = String.Empty;
            float productPrice = 0;
            short productQuantity = 1;
            if (String.IsNullOrEmpty(name))
                exceptions += "<br />Product name can't be empty.";
            if(String.IsNullOrEmpty(price))
                exceptions += "<br />Product price can't be empty.";
            if(String.IsNullOrEmpty(description))
                exceptions += "<br />Product description can't be empty.";
             if(String.IsNullOrEmpty(quantity))
                 exceptions += "<br />Product quantity can't be empty.";
            if(!String.IsNullOrEmpty(exceptions))
            {
                throw new ValidationException(exceptions);
            }
            var reg = new Regex(@"^[^< ]*[^>]*$");
            if (name != null)
            {
                if (!reg.IsMatch(name))
                {
                    exceptions += "<br />Product name contains illegal characters.";
                }
                if (name.Length < 10 || name.Length > 100)
                    exceptions += "<br />Product name must be between 10 and 100 characters.";
            }
            if (description != null)
            {
                if (!reg.IsMatch(description))
                {
                    exceptions += "<br />Product description contains illegal characters.";
                }
                if (description.Length < 50 || description.Length > 500)
                    exceptions += "<br />Product description must be between 50 and 500 characters.";
            }
            try
            {
                productPrice = float.Parse(price,CultureInfo.CurrentCulture);
                if (productPrice < 5 || productPrice > 50000)
                {
                    exceptions += "<br />Product price must be in range of (5-50000).";
                }
            }
            catch (FormatException)
            {
                exceptions += "<br />Product price is not a valid number.";
            }
            try
            {
                productQuantity = short.Parse(quantity, CultureInfo.CurrentCulture);
                if (productQuantity < 1 || productQuantity > 99)
                {
                    exceptions += "<br />Product quantity must be in range of (1-99).";
                }
            }
            catch (FormatException)
            {
                exceptions += "<br />Product quantity is not a valid number.";
            }
            if(!String.IsNullOrEmpty(exceptions))
            {
                throw new ValidationException(exceptions);
            }
            Product product = new Product
            {
                Name = name,
                Category = category,
                Currency = currency,
                Description = description,
                Price = productPrice,
                HasImages = hasImages,
                Postdate = DateTime.Now,
                Status = "OnSale",
                SellerId = userId,
                Quantity = productQuantity,
                IsShown = false
            };
            product = ProductDataAccess.InsertProduct(product);
            return product;
        }

    }
}
